﻿/*******************************************************************************
 * Copyright © 2016
 * 
 * Description: MVC快速开发平台  FROM http://xmwxgn.com
 *
*********************************************************************************/
using ZFine.Code;
using ZFine.Domain.Entity.Gas;
using ZFine.Domain.IRepository.Gas;
using ZFine.Repository.Gas;
using System;
using System.Collections.Generic;
using System.Linq;

namespace ZFine.Application.Gas
{
    public class ChargeRecordApp
    {
        private IChargeRecordRepository chargeRecordRepository = new ChargeRecordRepository();
        private ICompanyRepository companyRepository = new CompanyRepository();
        private IDispenserRepository dispenserRepository = new DispenserRepository();
        private IBottleRepository bottleRepository = new BottleRepository();

        /// <summary>
        /// 客户端插入
        /// </summary>
        /// <param name="chargeRecordEntity"></param>
        public void ClientInsert(ChargeRecordEntity chargeRecordEntity)
        {
            chargeRecordRepository.Insert(chargeRecordEntity);
        }
        public int GetListByChargeTypeAndDate(string ChargeType,DateTime begin,DateTime end)
        {
            var expression = ExtLinq.True<ChargeRecordEntity>();
            expression = expression.And(t => t.ChargeType== ChargeType);
            expression = expression.And(t => t.ChargeDate > begin);
            expression = expression.And(t => t.ChargeDate < end);
            return chargeRecordRepository.IQueryable(expression).Count();
        }


        public List<ChargeRecordEntity> GetList( string keyword="")
        {
            var expression = ExtLinq.True<ChargeRecordEntity>();
            if (!string.IsNullOrEmpty(keyword))
            {
                expression = expression.And(t => t.PlateNumber.Contains(keyword));
            }
            return chargeRecordRepository.IQueryable(expression).OrderBy(t => t.ChargeDate).ToList();
        }

        public int GetToDoListCount(string ChargeType, string RecordStatus)
        {
            return chargeRecordRepository.IQueryable(a=>a.RecordStatus==RecordStatus&&a.ChargeType== ChargeType).Count();
        }
        public int GetToDoListCount(string ChargeType)
        {
            return chargeRecordRepository.IQueryable(a =>  a.ChargeType == ChargeType).Count();
        }

        public List<ChargeRecordEntity> GetList(Pagination pagination, string keyword)
        {
            var expression = ExtLinq.True<ChargeRecordEntity>();
            if (!string.IsNullOrEmpty(keyword))
            {
                expression = expression.And(t => t.PlateNumber.Contains(keyword));
            }
            return chargeRecordRepository.FindList(expression, pagination);
        }
        
        /// <summary>
        /// 根据记录状态获得记录列表
        /// </summary>
        public List<ChargeRecordEntity> GetList(string chargeType, string recordStatus)
        {
            var expression = ExtLinq.True<ChargeRecordEntity>();
            if (!string.IsNullOrEmpty(recordStatus))
            {
                expression = expression.And(t => t.RecordStatus == recordStatus);
            }
            if (!string.IsNullOrEmpty(chargeType))
            {
                expression = expression.And(t => t.ChargeType == chargeType);
            }
            return chargeRecordRepository.IQueryable(expression).OrderBy(t => t.ChargeDate).ToList();
        }


        /// <summary>
        /// 根据记录状态获得记录列表 分页 关键字
        /// </summary>
        public List<ChargeRecordEntity> GetList(Pagination pagination, string keyword,string chargeType, string recordStatus)
        {
            var expression = ExtLinq.True<ChargeRecordEntity>();
            if (!string.IsNullOrEmpty(keyword))
            {
                expression = expression.And(t => t.PlateNumber.Contains(keyword));
            }
            if (!string.IsNullOrEmpty(recordStatus))
            {
                expression = expression.And(t => t.RecordStatus == recordStatus);
            }
            if (!string.IsNullOrEmpty(chargeType))
            {
                expression = expression.And(t => t.ChargeType == chargeType);
            }
            return chargeRecordRepository.FindList(expression, pagination);
        }
        public List<ChargeRecordEntity> GetListByUserId(Pagination pagination, string userid = "")
        {
            var expression = ExtLinq.True<ChargeRecordEntity>();
            if (!string.IsNullOrEmpty(userid))
            {
                expression = expression.And(t => t.AscertainId == userid);
                expression = expression.Or(t => t.CheckId == userid);
                expression = expression.Or(t => t.DealId == userid);
            }
            return chargeRecordRepository.FindList(expression, pagination);
        }

        public ChargeRecordEntity GetForm(string keyValue)
        {
            return chargeRecordRepository.FindEntity(keyValue);
        }
        public void DeleteForm(string keyValue)
        {
            //if (service.IQueryable().Count(t => t.F_ParentId.Equals(keyValue)) > 0)
            //{
            //    throw new Exception("删除失败！操作的对象包含了下级数据。");
            //}
            //else
            //{
            //    service.Delete(t => t.F_Id == keyValue);
            //}
        }
        public void SubmitForm(ChargeRecordEntity chargeRecordEntity, string keyValue)
        {
            if (!string.IsNullOrEmpty(keyValue))
            {
                chargeRecordEntity.Modify(keyValue);
                chargeRecordRepository.Update(chargeRecordEntity);
            }
            else
            {
                chargeRecordEntity.Create();
                chargeRecordRepository.Insert(chargeRecordEntity);
            }
        }
        /// <summary>
        /// 提交复核
        /// </summary>
        public void SubmitCheck(string recordid, string userid="")
        {
            if (string.IsNullOrEmpty(userid))
            {
                var LoginInfo = OperatorProvider.Provider.GetCurrent();
                if (LoginInfo != null)
                {
                    userid = LoginInfo.UserId;
                }
            }
            ChargeRecordEntity c = GetForm(recordid);
            c.RecordStatus = ZFine.Application.Enum.Gas.RecordStatus.提交复核.ToString();
            c.AscertainId = userid;
            c.AscertainDate = DateTime.Now;
            c.Modify(recordid);
            chargeRecordRepository.Update(c);
        }
        /// <summary>
        /// 消除误报
        /// </summary>
        public void CancelReport(string recordid, string userid = "")
        {
            if (string.IsNullOrEmpty(userid))
            {
                var LoginInfo = OperatorProvider.Provider.GetCurrent();
                if (LoginInfo != null)
                {
                    userid = LoginInfo.UserId;
                }
            }
            ChargeRecordEntity c = GetForm(recordid);
            c.RecordStatus = ZFine.Application.Enum.Gas.RecordStatus.误报消除.ToString();
            c.AscertainId = userid;
            c.AscertainDate = DateTime.Now;
            c.Modify(recordid);
            chargeRecordRepository.Update(c);
        }
        /// <summary>
        /// 申请决定意见
        /// </summary>
        public void AskView(string recordid, string userid = "")
        {
            if (string.IsNullOrEmpty(userid))
            {
                var LoginInfo = OperatorProvider.Provider.GetCurrent();
                if (LoginInfo != null)
                {
                    userid = LoginInfo.UserId;
                }
            }
            ChargeRecordEntity c = GetForm(recordid);
            c.RecordStatus = ZFine.Application.Enum.Gas.RecordStatus.申请决定意见.ToString();
            c.CheckId = userid;
            c.CheckDate = DateTime.Now;
            c.Modify(recordid);
            chargeRecordRepository.Update(c);
        }
        /// <summary>
        /// 驳回
        /// </summary>
        public void Reject(string recordid, string userid = "")
        {
            if (string.IsNullOrEmpty(userid))
            {
                var LoginInfo = OperatorProvider.Provider.GetCurrent();
                if (LoginInfo != null)
                {
                    userid = LoginInfo.UserId;
                }
            }
            ChargeRecordEntity c = GetForm(recordid);
            c.RecordStatus = ZFine.Application.Enum.Gas.RecordStatus.驳回.ToString();
            c.CheckId = userid;
            c.CheckDate = DateTime.Now;
            c.Modify(recordid);
            chargeRecordRepository.Update(c);
        }
        /// <summary>
        /// 处罚
        /// </summary>
        public void Punish(string recordid, string userid = "")
        {
            if (string.IsNullOrEmpty(userid))
            {
                var LoginInfo = OperatorProvider.Provider.GetCurrent();
                if (LoginInfo != null)
                {
                    userid = LoginInfo.UserId;
                }
            }
            ChargeRecordEntity c = GetForm(recordid);
            c.RecordStatus = ZFine.Application.Enum.Gas.RecordStatus.处罚.ToString();
            c.DealId = userid;
            c.DealDate = DateTime.Now;
            c.Modify(recordid);
            chargeRecordRepository.Update(c);
        }
        /// <summary>
        /// 处理消除
        /// </summary>
        public void Clear(string recordid, string userid = "")
        {
            if (string.IsNullOrEmpty(userid))
            {
                var LoginInfo = OperatorProvider.Provider.GetCurrent();
                if (LoginInfo != null)
                {
                    userid = LoginInfo.UserId;
                }
            }
            ChargeRecordEntity c = GetForm(recordid);
            c.RecordStatus = ZFine.Application.Enum.Gas.RecordStatus.处理消除.ToString();
            c.DealId = userid;
            c.DealDate = DateTime.Now;
            c.Modify(recordid);
            chargeRecordRepository.Update(c);
        }
    }
}
